package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.Sys_permissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Slf4j
public class Sys_permissionController {

    @Reference(version = "1.0.0")
    private Sys_permissionService sys_permissionService;

    @GetMapping("/permission")
    public Result queryPermissionList(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = this.sys_permissionService.queryPermissionList(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
    }

    @PostMapping("/permission")
    public Result savePermissions(@RequestBody Map<String,String> map){
        Boolean aBoolean = this.sys_permissionService.savePermissions(map);
        if(aBoolean){
            return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
    }

    @GetMapping("/permission/{id}")
    public Result queryEditPermissions(@PathVariable("id") Integer id){
        try {
            Permission permission = this.sys_permissionService.queryEditPermissions(id);
            if(ObjectUtil.isNotEmpty(permission)){
                return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
            }
        } catch (Exception e) {
            log.error("查询失败 id = "+ id,e);
        }
        return  new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
    }

    @PutMapping("/permission")
    public Result editPermissions(@RequestBody Permission permission){
        Boolean aBoolean = this.sys_permissionService.editPermissions(permission);
        if (aBoolean){
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS,permission);
        }
        return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
    }


    @DeleteMapping("/permission/{id}")
    public Result delPermissions(@PathVariable("id") Integer id){
        try {
            Boolean aBoolean = this.sys_permissionService.delPermissions(id);
            if(aBoolean){
                return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
            }
        } catch (Exception e) {
            log.error("删除失败 id = "+ id, e);
        }
        return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
    }


    @GetMapping("/permission/all")
    public Result queryPermissionsAll(){
        try {
            List<Permission> permissions = this.sys_permissionService.queryPermissionsAll();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissions);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
    }
}
